<script type="text/javascript">
    function submitMiscScheduleForm() {
        $.ajax({
            url:'index.php?r=contractMiscSchedule/update&contractid=' + contractid,

            type:'post',
            data:$('#miscSh').serialize(),
            success:function (data) {
                alert('Data Saved');

            }
        });
    }
</script>
<br/>
<div class="form">
    <h1>Close Monitoring Open/Close Schedule</h1>
    <br>
    <?php echo CHtml::form('', '', array('id' => 'miscSh')); ?>

    <?php
    $days = array(
        '1' => 'Monday',
        '2' => 'Tuesday',
        '3' => 'Wednesday',
        '4' => 'Thursday',
        '5' => 'Friday',
        '6' => 'Saturday',
        '7' => 'Sunday',
    );

    $criteria = new CDbCriteria();
    $criteria->condition = "contractid = '$model->contractid'";
    $criteria->order = 'day Asc';
    $scheduleItems = ContractMiscSchedule::model()->findAll($criteria);
    if (count($scheduleItems) < 7) {
        $scheduleItems = array();
        for ($i = 1; $i <= 7; $i++) {
            $scheduleItem = new ContractMiscSchedule();
            $scheduleItem->day = $i;
            $scheduleItem->contractid = $model->contractid;
            $scheduleItems[$i] = $scheduleItem;
        }
    }
    ?>
    <table border="1">
        <tr>
            <th>Day</th>
            <th>Open Time</th>
            <th>Close Time</th>
            <th>Unauth Open</th>
            <th>Fail to Open</th>
            <th>Fail to Close</th>
            <th>Late to Open</th>
        </tr>
        <?php foreach ($scheduleItems as $scheduleItem): ?>
        <tr>
            <td>
                <?php echo $days[$scheduleItem->day]; ?>
            </td>
            <td>
                <?php
                echo CHtml::textField("schedule[$scheduleItem->day][opentime]", $scheduleItem->opentime);

                ?>
            </td>
            <td>
                <?php
                echo CHtml::textField("schedule[$scheduleItem->day][closetime]", $scheduleItem->closetime);

                ?>
            </td>
            <td>
                <?php
                echo CHtml::textField("schedule[$scheduleItem->day][unauthopen]", $scheduleItem->unauthopen);

                ?>
            </td>
            <td>
                <?php
                echo CHtml::textField("schedule[$scheduleItem->day][failtoopen]", $scheduleItem->failtoopen);

                ?>
            </td>
            <td>
                <?php
                echo CHtml::textField("schedule[$scheduleItem->day][failtoclose]", $scheduleItem->failtoclose);

                ?>
            </td>
            <td>
                <?php
                echo CHtml::textField("schedule[$scheduleItem->day][latetoopen]", $scheduleItem->latetoopen);

                ?>
            </td>
        </tr>


        <?php endforeach; ?>
    </table>
    <br>

    <h1>Allowance Times</h1>
    <br/>
    <?php $labels = ContractMiscAllolabel::model()->findAll();
    $allowanceTimes = CHtml::listData(ContractMiscAllotime::model()->findAll("contractid = '$model->contractid'"), 'timeid', 'value');

    ?>

    <?php foreach ($labels as $times): ?>
    <div class="row">
        <?php echo CHtml::label($times->label, ''); ?>
        <?php if (isset($allowanceTimes[$times->id])) {
        echo CHtml::textField("Times[$times->id][value]", $allowanceTimes[$times->id]);
    } else {
        echo CHtml::textField("Times[$times->id][value]", '');

    }

        ?>
    </div>
    <?php endforeach; ?>


    <div class="row buttons">
        <input type="button" onclick="submitMiscScheduleForm(); return false;" value="submit"/>
    </div>

    <?php echo CHtml::endForm(); ?>

</div><!-- form -->